package light.mvc.controller.meetingroom;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.model.record.TroomSchedule;
import light.mvc.pageModel.base.Grid;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.meetingroom.MeetingroomEvent;
import light.mvc.pageModel.meetingroom.RoomPageModel;
import light.mvc.pageModel.notice.Training;
import light.mvc.pageModel.record.Room;
import light.mvc.service.notice.TrainingServiceI;
import light.mvc.service.record.RoomScheduleServiceI;
import light.mvc.service.record.RoomServiceI;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/order" })
public class OrderController extends BaseController {
	private static final Logger logger = Logger.getLogger(OrderController.class);
	@Autowired
	private TrainingServiceI trainingService;
	@Autowired
	private RoomScheduleServiceI roomScheduleService;
	@Autowired
	private RoomServiceI roomService;

	@RequestMapping({ "/meetingroom" })
	public String manager() {
		return "/meetingroom/order";
	}

	@RequestMapping({ "/addRoomUI" })
	public String addRoomUI() {
		return "/meetingroom/addRoomUI";
	}

	@RequestMapping({ "/roomAdd" })
	@ResponseBody
	public Json roomAdd(String roomName) {
		Json json = new Json();
		Room room = new Room();
		room.setName(roomName);
		try {
			roomService.add(room);
			json.setSuccess(true);
			json.setMsg("添加成功");
			logger.info(new Date() + " meeting room add success");
			
		} catch (Exception e) {
			logger.info(new Date() + " meeting room add failed");
			json.setSuccess(false);
			json.setMsg("添加失败");
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping({ "/delRoomUI" })
	public String delRoomUI(HttpServletRequest req) {
		List<Room> rooms;
		rooms = roomService.getAllRoom();
		req.setAttribute("rooms", rooms);
		return "/meetingroom/delRoomUI";
	}

	@RequestMapping({ "/roomDel" })
	@ResponseBody
	public Json roomDel(Long id) {
		Json json = new Json();
		roomService.delById(id);
		json.setSuccess(true);
		return json;
	}

	@RequestMapping({ "/modRoomUI" })
	public String modRoomUI(@ModelAttribute RoomPageModel roomPageModel) throws UnsupportedEncodingException {
		roomPageModel.setRoomName(URLDecoder.decode(roomPageModel.getRoomName(), "utf-8"));
		return "/meetingroom/modRoomUI";
	}

	@RequestMapping({ "/roomMod" })
	@ResponseBody
	public Json roomMod(RoomPageModel roomPageModel) {
		Json json = new Json();
		roomService.edit(roomPageModel);
		json.setSuccess(true);
		return json;
	}

	@RequestMapping({ "/modifyui" })
	public String modify(HttpServletRequest req, MeetingroomEvent roomEvent) throws UnsupportedEncodingException {
		MeetingroomEvent event = new MeetingroomEvent();
		event.setTitle(URLDecoder.decode(roomEvent.getTitle(), "utf-8"));
		event.setRoomName(URLDecoder.decode(roomEvent.getRoomName(), "utf-8"));
		event.setId(Integer.valueOf(roomEvent.getId()));

		MeetingroomEvent meetingevent = new MeetingroomEvent();
		meetingevent.setId(Integer.valueOf(roomEvent.getId()));
		TroomSchedule tr = (TroomSchedule) roomScheduleService.getEventById(meetingevent);
		event.setOwner(tr.getUser().getName());
		event.setOwnerId(tr.getUser().getId().intValue());
		event.setStart(tr.getStart());
		event.setEnd(tr.getEnd());
		req.setAttribute("roomEvent", event);
		return "/meetingroom/orderModify";
	}

	@RequestMapping({ "/dataGrid" })
	@ResponseBody
	public Grid dataGrid(Training training, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(this.trainingService.dataGrid(training, ph));
		grid.setTotal(this.trainingService.count(training, ph));
		return grid;
	}
}
